<?php
include("../safe.php");
$lang=$_GET["lang"];
$bname=$_GET["bname"];
$page=$_GET["page"];
$platform=$_GET["platform"];
if($lang!==''&&isset($lang)){}else{$lang="products";}
$rss="select * from ph_".$lang." where ID='".intval($_GET["id"])."'";
$rsresult=mysqli_query($conn,$rss);
$rsrows=mysqli_fetch_array($rsresult);
$sql="select * from ph_admin where admin='".$_SESSION["managername"]."'";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
?> 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
<title><?php echo $site_name;?>后台管理-编辑产品</title>
<link href="../bootstrap/css/bootstrap.min.css" title="" rel="stylesheet" />
<link href="../css/mcommon.css" rel="stylesheet" title="" type="text/css" />
<link href="../css/style.css" rel="stylesheet" type="text/css"  />
<link rel="shortcut icon" href="/logo.ico" />
<style type="text/css">
<!--
table{margin:0px; padding:0px; border:0px;}
form,h3{ margin:0px; padding:0px;}
.tab-block {
	width:100%;
	/*height: 580px;*/
	background-color: #FFF;
	border: 1px solid #e5e5e5;
	margin:auto;
}

.tab-block .tab-buttons {
	width: 100%;
	height: 50px;
	background-color: #F0F0F0;
}

.tab-block .tab-buttons .tab-button {
	width: 33.33%;
	float: left;
	height: 50px;
	text-align: center;
	font-size: 16px;
	color: #999;
	line-height: 50px;
	position: relative;
	cursor: pointer
}

.tab-block .tab-buttons .tab-button.cur {
	background-color: #fff;
	color: #333
}

.tab-block .tab-buttons .tab-button.cur:after {
	content: '';
	position: absolute;
	top: -1px;
	left: 0;
	width: 100%;
	height: 2px;
	background-color: #1183ff
}

.tab-block .tabs {
	width: 100%;
	/*min-height: 528px*/
}

.tab-block .tabs .tab-item {
	width: 100%;
	height: 100%;
	display: none
}

.tab-block .tabs .tab-item.active {
	display: block
}
-->
</style>
<script src="../js/jquery-1.11.1.min.js" type="text/javascript"></script>
<script src="../js/ajaxfileupload.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var id=0;
$(document).ready(function(){
    addproimg();
    bindListener();
	<?php
	$slqa="select * from ph_img where id in(select imgid from(select imgid from ph_proimg where proid='".intval($_GET["id"])."' and btid is null order by px,id asc) t order by px asc);";
$rsresulta=mysqli_query($conn,$slqa);
while($rowa=mysqli_fetch_array($rsresulta)){
echo '$("#productimg").append(\'<table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#CCCCCC" class="wenzi"><tr><td bgcolor="#F7F7F7"><input name="proimg[]" type="hidden" id="proimg[]" value="'.$rowa["imgpath"].'"/><img src="'.$rowa["imgpath"].'" style="width:100px;"></td><td height="25" bgcolor="#F7F7F7"><input class="form-control" name="px[]" type="number" id="px[]" value="'.$rowa["px"].'" size="30" /></td><td bgcolor="#F7F7F7"><a class="btn btn-danger" role="button" href="#" name="delimg">删除</a> <a class="btn btn-info" role="button" href="imgtool.php?img='.$rowa["imgpath"].'" target="_blank">去水印</a></td></tr></table>\');';
echo 'bindListener();';
}

	$slqb="select * from ph_shuxing where proid='".intval($_GET["id"])."' order by ID asc";
$rsresultb=mysqli_query($conn,$slqb);
while($rowb=mysqli_fetch_array($rsresultb)){
echo '$("#mdiv").append(\'<table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#CCCCCC" class="wenzi"><tr><td width="25%" bgcolor="#F7F7F7"><input class="form-control" name="btname[]" type="text" id="btname[]" value="'.$rowb["cname"].'"/> '.$rowb["sid"].'变体名，双变体用","隔开</td><td width="16%" height="25" bgcolor="#F7F7F7"><input class="form-control" name="btprice[]" type="number" id="btprice[]" value="'.$rowb["price"].'" step="0.001"/> 默认价格</td><td width="16%" bgcolor="#F7F7F7"><input class="form-control" name="btsaleprice[]" type="number" id="btsaleprice[]" value="'.$rowb["saleprice"].'" step="0.001"> 销售价格</td><td width="16%" bgcolor="#F7F7F7"><input class="form-control" name="upc[]" type="text" id="upc[]" value="'.$rowb["skucode"].'"> SKU码</td><td width="16%" bgcolor="#F7F7F7"><input class="form-control" name="btkucun[]" type="number" id="btkucun[]" value="'.$rowb["kucun"].'"> 库存数量</td><td bgcolor="#F7F7F7"><a class="btn btn-default" role="button" href="#" name="rmlink">删除变体</a></td></tr><tr><td bgcolor="#F7F7F7"><input type="file" id="btn_file\'+ id +\'" name="btn_file\'+ id +\'" style="display:none" onchange="upload_cover(\'+ id +\')"><input class="btn btn-default" type="button" value="新增图片" id="\'+ id +\'"><input name="imgid[]" type="hidden" id="imgid[]" value="\'+ id +\'" /></td><td height="25" colspan="5" bgcolor="#F7F7F7"><div id="myimg\'+ id +\'"></div></td></tr></table>\');';
echo '$("#"+id).click(function(){$("#btn_file"+$(this).attr(\'id\')).click();});';
//echo "id=".$i.";";
$slqc="select * from ph_img where id in(select imgid from(select imgid from ph_proimg where btid='".$rowb["ID"]."' order by id asc) t order by id asc);";
//echo $slqc;
$rsresultc=mysqli_query($conn,$slqc);
while($rowc=mysqli_fetch_array($rsresultc)){
echo '$("#myimg"+id).append(\'<div style="width:180px; float:left"><input name="btimg\'+ id +\'[]" type="hidden" id="btimg\'+ id +\'[]" value="'.$rowc["imgpath"].'"/><img src="'.$rowc["imgpath"].'" style="width:100px;"><a href="#" name="delbtimg">删除</a> | <a href="imgtool.php?img='.$rowc["imgpath"].'" target="_blank">去水印</a></div>\');';
echo 'bindListener();';
}
echo 'id++;';
echo 'bindListener();';
}
?>
});
function addimg(){
    $("#mdiv").append('<table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#CCCCCC" class="wenzi"><tr><td width="25%" bgcolor="#F7F7F7"><input class="form-control" name="btname[]" type="text" id="btname[]" /> 变体名，双变体用","隔开</td><td width="16%" height="25" bgcolor="#F7F7F7"><input class="form-control" name="btprice[]" type="number" id="btprice[]" step="0.001"/> 默认价格</td><td width="16%" bgcolor="#F7F7F7"><input class="form-control" name="btsaleprice[]" type="number" id="btsaleprice[]" step="0.001"> 销售价格</td><td width="16%" bgcolor="#F7F7F7"><input class="form-control" name="upc[]" type="text" id="upc[]"> SKU码</td><td width="16%" bgcolor="#F7F7F7"><input  class="form-control" name="btkucun[]" type="number" id="btkucun[]"> 库存数量</td><td bgcolor="#F7F7F7"><a class="btn btn-default" role="button" href="#" name="rmlink">删除变体</a></td></tr><tr><td bgcolor="#F7F7F7"><input type="file" id="btn_file'+ id +'" name="btn_file'+ id +'" style="display:none" onchange="upload_cover('+ id +')"><input class="btn btn-default" type="button" value="新增图片" id="'+ id +'"><input name="imgid[]" type="hidden" id="imgid[]" value="'+ id +'" /></td><td height="25" colspan="5" bgcolor="#F7F7F7"><div id="myimg'+ id +'"></div></td></tr></table>');
	
	$("#"+id).click(function(){
        //$("#myimg"+$(this).attr('id')).append('<input name="btimg'+ $(this).attr('id') +'[]" type="hidden" id="btimg[]"/>');
		 $("#btn_file"+$(this).attr('id')).click();
		 //upload($(this).attr('id'));
		  //alert($(this).attr('id'));
    });
	id++;
    // 为新元素节点添加事件侦听器
    bindListener();
}
function addproimg(){
  $("#proimgdiv").append('<div style="padding-left:10px;"><input type="file" id="btn_file" name="btn_file" style="display:none" onchange="upload_img()"><input class="btn btn-default" type="button" value="新增图片" id="jiaimg"><div id="productimg"></div></div>');
  $("#jiaimg").click(function(){
        //$("#myimg"+$(this).attr('id')).append('<input name="btimg'+ $(this).attr('id') +'[]" type="hidden" id="btimg[]"/>');
		 $("#btn_file").click();
		 //upload($(this).attr('id'));
		  //alert($(this).attr('id'));
    });
    // 为新元素节点添加事件侦听器
    bindListener();
}
// 用来绑定事件(使用unbind避免重复绑定)
function bindListener(){
    $("a[name=rmlink]").unbind().click(function(){
        $(this).parent().parent().parent().remove();
		//id--;
    });
	$("a[name=delimg]").unbind().click(function(){
        $(this).parent().parent().parent().remove();
		
    });
	$("a[name=delbtimg]").unbind().click(function(){
        $(this).parent().remove();
    });
}

function plprice(form)
{
  for (var i=0;i<form.elements.length;i++)
  {
  var e = form.elements[i];
  if (e.name == "btsaleprice[]"&&e.disabled!=true)
  e.value = form.zbtsaleprice.value;
  }
}
function plkucun(form)
{
  for (var i=0;i<form.elements.length;i++)
  {
  var e = form.elements[i];
  if (e.name == "btkucun[]"&&e.disabled!=true)
  e.value = form.plbtkucun.value;
  }
}
</script>
</head>

<body>
<!--顶部-->
<?php include("../top.php");?> 
<!--顶部结束--->
<!--主体部分-->
<div class="down-main">
	<!--左侧部分-->
  <?php include("../left.php");?> 
  <!--左侧部分结束-->
  <!--右侧部分-->
  <div class="right-product right-full">
  	<!--右侧内容部分-->
    <div class="container-fluid right-product-con">
    	<div class="mianbaoxie"><i class="glyphicon-folder-open glyphicon"></i>
    		<span>管理中心</span>&gt;<span>产品管理</span>&gt;<span><?php echo $_GET["bname"];?></span>&gt;<span class="dangqian">编辑产品</span></div>
      <div class="container-con">
      		<!--表格-->
      		<div class="tableCon">
			<div class="tableCon-tit clear">编辑产品</div>
	      		<div class="tableCon-content">
				
				<div class="tab-block information-tab">
			<div class="tab-buttons ">
				<h3 class="tab-button cur" data-tab="one">基本信息</h3>
				<h3 class="tab-button" data-tab="two">商品变体与价格</h3>
				<h3 class="tab-button" data-tab="three">图片信息</h3>
			</div>
			<div class="tabs">
			<form action="edit_save.php?id=<?php echo $rsrows["ID"];?>&lang=<?php echo $lang;?>&platform=<?php echo $platform;?>" method="post" enctype="multipart/form-data" name="myform" id="myform" class="form-inline">
				<div class="tab-item active" id="tab-one">
					<div class="information-tab">
					 <table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#CCCCCC" class="wenzi">
      <tr>
        <td height="25" colspan="2" bgcolor="#FFFFFF"><div align="left" style="color:#999999;">产品编辑(*为必填项，！为提示)</div></td>
      </tr>
      <tr>
        <td width="16%" bgcolor="#F7F7F7"><div align="right">产品分类：</div></td>
        <td width="84%" height="25" bgcolor="#F7F7F7"><div align="left">
          <select name="bname" id="bname" class="form-control">
		  <option value="<?php echo $rsrows["bname"];?>"><?php echo $rsrows["bname"];?></option>
            <?php
		  $querys="select * from ph_bcategory where user_name='".$_SESSION["managername"]."'";
          $results=mysqli_query($conn,$querys);
          while($rows=mysqli_fetch_array($results)){
		?>
            <option value="<?php echo $rows["bname"];?>"><?php echo $rows["bname"];?></option>
            <?php }?>
          </select>
        *</div></td>
      </tr>
      <tr>
        <td bgcolor="#F7F7F7"><div align="right">产品名称：</div></td>
        <td height="25" bgcolor="#F7F7F7"><div align="left">
          <input name="proname" type="text" id="proname" size="160" value="<?php echo $rsrows["proname"];?>" class="form-control"/>
*</div></td>
      </tr>
	  <tr>
        <td bgcolor="#F7F7F7"><div align="right">产品来源URL：</div></td>
        <td height="25" bgcolor="#F7F7F7"><div align="left">
          <input name="fromurl" type="text" id="fromurl" size="160" class="form-control" value="<?php echo $rsrows["fromurl"];?>"/>
</div></td>
      </tr>
      <tr>
        <td bgcolor="#F7F7F7"><div align="right">SKU码：</div></td>
        <td height="25" bgcolor="#F7F7F7"><input name="skucode" type="text" id="skucode" size="30" value="<?php echo $rsrows["skucode"];?>" class="form-control"/></td>
      </tr>
      <tr>
        <td bgcolor="#F7F7F7"><div align="right">默认价格：</div></td>
        <td height="25" bgcolor="#F7F7F7"><div align="left">
          <input name="price" type="number" id="price" size="30" value="<?php echo $rsrows["price"];?>" step="0.001" class="form-control"/>
        </div></td>
      </tr>
      <tr>
        <td bgcolor="#F7F7F7"><div align="right">销售价格：</div></td>
        <td height="25" bgcolor="#F7F7F7"><input name="saleprice" type="number" id="saleprice" size="30" value="<?php echo $rsrows["saleprice"];?>" step="0.001" class="form-control"/></td>
      </tr>
      <tr>
        <td bgcolor="#F7F7F7"><div align="right">币种：</div></td>
        <td height="25" bgcolor="#F7F7F7"><div align="left">
          <input name="coin" type="text" id="coin" size="30" value="<?php echo $rsrows["coin"];?>" class="form-control"/>
        </div></td>
      </tr>
      
      <tr>
        <td bgcolor="#F7F7F7"><div align="right">编号：</div></td>
        <td height="25" bgcolor="#F7F7F7"><div align="left">
          <input name="code" type="text" id="code" value="<?php echo $rsrows["code"];?>" size="30" readonly="readonly" class="form-control"/></div></td>
      </tr>
      <tr>
        <td bgcolor="#F7F7F7"><div align="right">库存：</div></td>
        <td height="25" bgcolor="#F7F7F7"><div align="left">
          <input name="kucun" type="number" id="kucun" value="<?php echo $rsrows["kucun"];?>" size="30" class="form-control"/>
        </div></td>
      </tr>
      <tr>
        <td bgcolor="#F7F7F7"><div align="right">重量：</div></td>
        <td height="25" bgcolor="#F7F7F7"><div align="left">
          <input name="weight" type="number" id="weight" value="<?php echo $rsrows["weight"];?>" size="30" class="form-control"/>
        kg</div></td>
      </tr>
      <tr>
        <td bgcolor="#F7F7F7"><div align="right">卖点：</div></td>
        <td height="25" bgcolor="#F7F7F7"><div align="left">
          <textarea id="sketch" name="sketch" cols="100" rows="5" style="width:100%;" class="form-control"><?php echo $rsrows["sketch"];?></textarea>
        </div></td>
      </tr>
      <tr>
        <td bgcolor="#F7F7F7"><div align="right">关键词：</div></td>
        <td height="25" bgcolor="#F7F7F7"><div align="left">
          <textarea id="keywords" name="keywords" cols="100" rows="5" style="width:100%;" class="form-control"><?php echo $rsrows["keywords"];?></textarea>
        </div></td>
      </tr>
      <tr>
        <td bgcolor="#F7F7F7"><div align="right">产品说明：</div></td>
        <td height="25" bgcolor="#F7F7F7"><div align="left">
          <textarea id="content" name="content" cols="100" rows="8" style="width:100%;" class="form-control"><?php echo $rsrows["content"];?></textarea>
        </div></td>
      </tr> 
      <tr>
        <td bgcolor="#F7F7F7"><div align="right"></div></td>
        <td height="25" bgcolor="#F7F7F7"><div align="left">
         <input type="submit" class="btn btn-success" value="确定保存"/>
        <a class="btn btn-info" role="button" href="productslist.php?bname=<?php echo $bname;?>&lang=<?php echo $lang;?>&page=<?php echo $page;?>&platform=<?php echo $platform;?>">返回列表</a>
        <input name="upload" type="hidden" id="upload" value="Send" />
        </div></td>
      </tr>
  </table>
					</div>
				</div>
				<div class="tab-item" id="tab-two">
					<div class="information-tab ">
                     <div style="height:40px; line-height:40px; color:#000000; padding-left:10px;"><input type="button" class="btn btn-success" value="新增变体" onClick="javascript:addimg();"/> 变体类名：<input class="form-control" name="bttname" id="bttname" type="text" value="<?php echo $rsrows["btname"];?>"/></div>
<div id="mdiv"><table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#CCCCCC" class="wenzi"><tr><td width="25%" bgcolor="#F7F7F7">&nbsp;</td><td width="16%" height="25" bgcolor="#F7F7F7">&nbsp;</td><td width="16%" bgcolor="#F7F7F7"><input class="form-control" name="zbtsaleprice" type="number" id="zbtsaleprice" step="0.001" onChange="plprice(this.form)"> 销售价格</td><td width="16%" bgcolor="#F7F7F7">&nbsp;</td><td width="16%" bgcolor="#F7F7F7"><input class="form-control" name="plbtkucun" type="number" id="plbtkucun" onChange="plkucun(this.form)"> 库存数量</td><td bgcolor="#F7F7F7">&nbsp;</td></tr></table></div>
<div style="height:40px; line-height:40px; color:#000000; padding-left:10px;">
<input type="submit" class="btn btn-success" value="确定保存"/>
<a class="btn btn-info" role="button" href="productslist.php?bname=<?php echo $bname;?>&lang=<?php echo $lang;?>&page=<?php echo $page;?>&platform=<?php echo $platform;?>">返回列表</a></div>
					</div>
				</div>
				<div class="tab-item" id="tab-three">
					<div class="information-tab ">
<div id="proimgdiv"></div>
<div style="height:40px; line-height:40px; color:#000000; padding-left:10px;"><input type="submit" class="btn btn-success" value="确定保存"/>	
<a class="btn btn-info" role="button" href="productslist.php?bname=<?php echo $bname;?>&lang=<?php echo $lang;?>&page=<?php echo $page;?>&platform=<?php echo $platform;?>">返回列表</a></div>
					</div>
				</div>
				</form>
			</div>
		</div>
		<script type="text/javascript">
 function upload_cover(obj) {
        ajax_upload("btn_file"+obj, function(data){ //function(data)是上传图片的成功后的回调方法
            //var isrc = data.relatPath.replace(/\/\//g, '/'); //获取的图片的绝对路径
			$("#myimg"+obj).append('<div style="width:180px; float:left"><input name="btimg'+ obj +'[]" type="hidden" id="btimg'+ obj +'[]" value="'+ data.files[0].path +'"/><img src="'+ data.files[0].path +'" style="width:100px;"><a href="#" name="delbtimg">删除</a> | <a href="imgtool.php?img='+ data.files[0].path +'" target="_blank">去水印</a></div>');		
			//alert(obj);
			bindListener();
        });
    }
	function upload_img() {
	ajax_upload("btn_file", function(data){
	   $("#productimg").append('<table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#CCCCCC" class="wenzi"><tr><td bgcolor="#F7F7F7"><input name="proimg[]" type="hidden" id="proimg[]" value="'+ data.files[0].path +'"/><img src="'+ data.files[0].path +'" style="width:100px;"></td><td height="25" bgcolor="#F7F7F7"><input class="form-control" name="px[]" type="number" id="px[]" value="0" size="30" /></td><td bgcolor="#F7F7F7"><a class="btn btn-danger" role="button" href="#" name="delimg">删除</a> <a class="btn btn-info" role="button" href="imgtool.php?img='+ data.files[0].path +'" target="_blank">去水印</a></td></tr></table>');
	   bindListener();
	   });
	}
    function ajax_upload(feid, callback) { //具体的上传图片方法
        if (image_check(feid)) { //自己添加的文件后缀名的验证
            $.ajaxFileUpload({
                fileElementId: feid,    //需要上传的文件域的ID，即<input type="file">的ID。
                url:'uploader.php?uid=<?php echo $row["ID"];?>', //后台方法的路径mg
                type: 'post',   //当要提交自定义参数时，这个参数要设置成post
                dataType: 'json',   //服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。
                secureuri: false,   //是否启用安全提交，默认为false。
                async : true,   //是否是异步
                success: function(data) {   //提交成功后自动执行的处理函数，参数data就是服务器返回的数据。
                    if(callback)callback.call(this, data);
                },
                error: function(data, status, e) {  //提交失败自动执行的处理函数。
                    console.error(e);
                }
            });
        }
    }
    function image_check(feid) { //自己添加的文件后缀名的验证
        var img = document.getElementById(feid);
        return /.(jpg|png|gif|bmp)$/.test(img.value)?true:(function() {
            alert('图片格式仅支持jpg、png、gif、bmp格式，且区分大小写。');
            return false;
        })();
    }

</script>
<script type="text/javascript">
	//tab切换		
	$('.tab-button').click(function() {
		var tab = $(this).data('tab')
		$(this).addClass('cur').siblings('.tab-button').removeClass('cur');
		$('#tab-' + tab + '').addClass('active').siblings('.tab-item').removeClass('active');
	});
</script>

	      		</div>
	      	</div>
	      	<!--表格结束--->
      	</div>
      </div>
  </div>
	  <!--右侧内容部分结束-->
  </div>
  <!--右侧部分结束-->
</div>
<!--主体部分结束-->
<script src="../bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="../js/mcommon.js"></script>
</body>
</html>
